package com.finger.educms.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.finger.commonutils.R;
import com.finger.educms.entity.CrmBanner;
import com.finger.educms.entity.vo.BannerQuery;
import com.finger.educms.service.CrmBannerService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * banner表接口，后端管理员使用
 * </p>
 *
 * @author finger
 * @since 2022-01-12
 */
@CrossOrigin
@RestController
@RequestMapping("/educms/banneradmin")
public class BannerAdminController {

    @Autowired
    private CrmBannerService crmBannerService;

    /**
     * 根据条件查询banner
     * @param bannerQuery 查询条件
     * @param page 当前页
     * @param size 页面大小
     * @return
     */
    @ApiOperation(value = "根据条件查询幻灯片")
    @PostMapping("/queryBannerCondition/{page}/{size}")
    public R queryTeacherByCondition(@RequestBody(required = false) BannerQuery bannerQuery,
                                     @ApiParam(name = "page", value = "当前页", defaultValue = "1") @PathVariable(value = "page") Long page,
                                     @ApiParam(name = "size", value = "当前页", defaultValue = "3") @PathVariable(value = "size") Long size) {
        String title = bannerQuery.getTitle();

        String sort = bannerQuery.getSort();

        String begin = bannerQuery.getBegin();

        String end = bannerQuery.getEnd();

        QueryWrapper<CrmBanner> pageWrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(title)) {
            pageWrapper.like("title", title);
        }
        if (!StringUtils.isEmpty(sort)) {
            pageWrapper.eq("sort", sort);
        }
        if (!StringUtils.isEmpty(begin)) {
            pageWrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            pageWrapper.le("gmt_modified", end);
        }

        //排序
        pageWrapper.orderByDesc("gmt_create");

        Page<CrmBanner> eduBannerPage = new Page<>(page, size);

        crmBannerService.page(eduBannerPage, pageWrapper);

        return R.ok().data("items", eduBannerPage.getRecords()).data("total",eduBannerPage.getTotal());
    }

    @GetMapping("/queryBanner/{id}")
    public R queryBanner(@PathVariable String id){

        CrmBanner crmBanner = crmBannerService.getById(id);

        return R.ok().data("banner",crmBanner);
    }

    @PutMapping("/updateBanner")
    public R updateBanner(@RequestBody CrmBanner crmBanner){

        crmBannerService.updateById(crmBanner);

        return R.ok();
    }

    @DeleteMapping("/deleteBanner/{id}")
    public R deleteBanner(@PathVariable String id){
        crmBannerService.removeById(id);
        return R.ok();
    }
    @PostMapping("/addBanner")
    public R addBanner(@RequestBody CrmBanner banner){
        crmBannerService.save(banner);
        return R.ok();
    }
}

